/*
 * Copyright (c) 2007 jMouse Gestures
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMouse Gestures' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jmg.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;


/**
 * TrailFadeTimer class.
 *
 * @author  Christopher Martin
 * @version $Id: TrailFadeTimer.java 9 2007-05-14 09:26:27Z GentlemanHal $
 */
public class TrailFadeTimer implements 
    ActionListener
{
    private final Timer timer;
    private final GestureTrailDrawer drawer;
    
    
    
    
    
    /**
     * Creates a new instance of <code>TrailFadeTimer</code>.
     *
     * @param  updateEverySeconds Updates every this number of seconds.  A
     *         simple way to set would be to use:
     *         <pre>
     *         drawer.getFader().getFadeTime() / X
     *         </pre>
     *         With <code>X</code> being any number greater than zero.  
     *         The higher <code>X</code> is the more the drawer will be updated 
     *         and the smoother and nicer the fade effect will appear.  
     *         A value of <code>X</code> around ten should work for most 
     *         applications.
     * @param  drawer The drawer that will be updated by this timer.
     * @throws java.lang.IllegalArgumentException If updateEverySeconds is less
     *         than or equal to zero or a <code>null</code> argument is
     *         passed.
     */
    public TrailFadeTimer(float updateEverySeconds, GestureTrailDrawer drawer) 
        throws IllegalArgumentException
    {
        if (null == drawer) {
            throw new IllegalArgumentException("drawer was null!");
        }
        if (updateEverySeconds <= 0f) {
            throw new IllegalArgumentException("updateEverySeconds must be > 0: " 
                                               + updateEverySeconds);
        }
        
        
        timer = new Timer((int) updateEverySeconds * TrailFader.SECONDS_TO_MILLISECONDS,
                          this);
        timer.setRepeats(true);
        this.drawer = drawer;
    }

    
    
    
    /**
     * Starts this timer running.
     */
    public void start() {
        if (timer.isRunning()) {
            timer.restart();
        } else {
            timer.start();
        }
    }
    
    /**
     * Stops the timer running.
     */
    public void stop() {
        if (timer.isRunning()) {
            timer.stop();
        }
    }
    
    /**
     * Method called to update the drawer.  This method is called internally
     * and should not be called by other classes.
     * 
     * @param e Not used.
     */
    public void actionPerformed(ActionEvent e) {
        drawer.paint();
    }
    
}
